//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Query Application Block
//===============================================================================

using System;
using System.IO;
using System.Globalization;
using EntLibContrib.Query.Instrumentation;
using EntLibContrib.Query.Properties;

namespace EntLibContrib.Query.Queries
{
	/// <summary>
	/// Abstract File based Query.
	/// </summary>
	/// <remarks>
	/// This provider is the base for all file-based queries
	/// </remarks>
	public abstract class FileQuery : QueryBase
	{
		#region Constants
		private const QueryType queryType = QueryType.File;
		#endregion

		#region Fields
		private string dataFile;
		#endregion

		#region Properties
		/// <summary>
		/// Gets the type of the query.
		/// </summary>
		/// <value>The type of the query.</value>
		public override QueryType QueryType
		{
			get { return queryType; }
		}

		/// <summary>
		/// Gets or sets the data file.
		/// </summary>
		/// <value>The data file.</value>
		public string DataFile
		{
			get { return dataFile; }
			set { dataFile = value; }
		}
		#endregion

		#region Construction
		/// <summary>
		/// Initializes a new default instance of the <see cref="FileQuery"/> class.
		/// </summary>
		/// <param name="name">The query name.</param>
		/// <param name="dataFile">The data file.</param>
		/// <param name="customCommand">The custom command.</param>
		/// <param name="parameterSet">The parameter set.</param>
		/// <param name="instrumentationProvider">The instrumentation provider.</param>
		protected FileQuery(string name,
												string dataFile,
												ICustomCommand customCommand,
												IParameterSet parameterSet,
												QueryInstrumentationProvider instrumentationProvider)
			: base(name, customCommand, parameterSet, instrumentationProvider)
		{
			ValidateAndStoreParameters(dataFile);
		}
		#endregion

		#region Private Methods
		/// <summary>
		/// Validates the and store parameters.
		/// </summary>
		/// <param name="filename">The filename.</param>
		private void ValidateAndStoreParameters (string filename)
		{
			// check for a null or empty path name
			if (string.IsNullOrEmpty(filename))
				throw new ArgumentNullException("filename", InternalResources.ExceptionFileQueryDataFileNullOrEmpty);

			// check that the file represents a valid path
			FileInfo fileInfo = new FileInfo(filename);
			if ( !fileInfo.Exists )
				throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, InternalResources.ExceptionFileQueryDataFileInvalid, filename), filename);

			// store the file name
			this.dataFile = filename;
		}
		#endregion
	}
}
